package com.mingqi.enetconnected.net.request;

import android.content.Context;

import com.google.gson.reflect.TypeToken;
import com.mingqi.enetconnected.base.MyApplication;
import com.mingqi.enetconnected.data.bean.order.OrderBrief;
import com.mingqi.enetconnected.data.bean.order.OrderDetail;
import com.mingqi.enetconnected.net.model.GeneralResponse;
import com.mingqi.enetconnected.net.model.randomcode.RandomCode;
import com.mingqi.enetconnected.net.util.Convert;
import com.mingqi.enetconnected.net.util.DataTest;
import com.mingqi.enetconnected.net.util.HttpParameters;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

/**
 *
 */
public class OrderItemRemoteRequest {
    /**
     * 查询我的工单列表URL
     */
    public final static String ORDERITEM_LIST_URL = "/v1/crm-order/orderItems/app";
    /**
     * 查询可抢工单列表URL
     */
    public final static String RUSH_ORDERITEM_LIST_URL = "/v1/crm-order/orderItems/rush";

    /**
     * 查询工单详情
     */
    public final static String ORDERITEM_DETAIL_URL = "/v1/crm-order/orderItems/";
    /**
     * 查询预约计划
     */
    public final static String ORDERPLAN_URL = "/v1/crm-order/orderPlan";
    /**
     * 查询用户业务办理条件
     */
    public final static String ORDER_PRECONDITIONS = "/v1/crm-order/orderItems/preconditions";
    /**
     * 查询工单可执行的事件
     */
    public final static String ORDER_EVENTS = "/v1/crm-order/orderItems/%s/events";
    /**
     * 签订合同地址
     */
    public final static String CONTRACT_URL = "https://sgp.towngasvcc.com/contract/%s?callbackUrl=" + HttpParameters.APP_SERVER_BASE_URL;

    /**
     * 查询我的工单列表
     *
     * @param cxt
     * @param bizCode    业务类型
     * @param orderState 工单状态
     * @param bookTime   预约时间
     * @param pageIndex  页码
     * @param pageSize   分页的大小
     */
    public static GeneralResponse<List<OrderBrief>> getOrderItemsList(Context cxt, String bizCode, char orderState, String bookTime, int pageIndex, int pageSize) throws Exception {
        Type type = new TypeToken<GeneralResponse<List<OrderBrief>>>() {
        }.getType();
        Communicator<GeneralResponse<List<OrderBrief>>> communicator = new Communicator<>(cxt, ORDERITEM_LIST_URL);
        HashMap<String, String> params = new HashMap<>();
        params.put("bizCode", bizCode);
        params.put("orderState", String.valueOf(orderState));
        params.put("bookTime", bookTime);
        params.put("pageIndex", String.valueOf(pageIndex));
        params.put("pageSize", String.valueOf(pageSize));
        if (MyApplication.isDebug) {
            return DataTest.getOrderItemsList();
        }
        return communicator.get(params, type);

    }



    /**
     * 查询可抢工单列表
     *
     * @param cxt
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @return
     * @throws Exception
     */
    public static GeneralResponse<List<OrderBrief>> getRushOrderItemsList(Context cxt, int pageIndex, int pageSize) throws Exception {
        Type type = new TypeToken<GeneralResponse<List<OrderBrief>>>() {
        }.getType();
        Communicator<GeneralResponse<List<OrderBrief>>> communicator = new Communicator<>(cxt, RUSH_ORDERITEM_LIST_URL);
        HashMap<String, String> params = new HashMap<>();
        params.put("pageIndex", String.valueOf(pageIndex));
        params.put("pageSize", String.valueOf(pageSize));
        return communicator.get(params, type);
    }

    /**
     * 查询工单详情
     *
     * @param cxt
     * @param orderCode 工单编码
     * @return
     * @throws Exception
     */
    public static OrderDetail getOrderItemDetail(Context cxt, String orderCode) throws Exception {
        Type type = new TypeToken<OrderDetail>() {
        }.getType();
        Communicator<OrderDetail> communicator = new Communicator<>(cxt, ORDERITEM_DETAIL_URL + orderCode);
        if (MyApplication.isDebug) {
            return DataTest.getOrderItemDetail();
        }
        return communicator.get(type);
    }

    /**
     * 查询预约计划
     *
     * @param cxt
     * @param subsCode   户号
     * @param bizCode    业务类型
     * @param subBizCode 二级业务编码
     * @return
     * @throws Exception
     */
    public static List<Object> getOrderPlan(Context cxt, String subsCode, String bizCode, String subBizCode) throws Exception {
        Type type = new TypeToken<List<Object>>() {
        }.getType();
        Communicator<List<Object>> communicator = new Communicator<>(cxt, ORDERPLAN_URL);
        HashMap<String, String> params = new HashMap<>();
        params.put("subsCode", subsCode);
        params.put("bizCode", bizCode);
        params.put("subBizCode", subBizCode);
        if (MyApplication.isDebug) {
            return DataTest.getOrderPlan();
        }

        return communicator.get(params, type);
    }

    /**
     * 查询用户业务办理条件
     *
     * @param cxt
     * @param subsCode   户号
     * @param bizCode    业务类型
     * @param subBizCode 二级业务编码
     * @return
     * @throws Exception
     */
    public static Object getOrderPreconditions(Context cxt, String subsCode, String bizCode, String subBizCode) throws Exception {
        Type type = new TypeToken<Object>() {
        }.getType();
        Communicator<Object> communicator = new Communicator<>(cxt, ORDER_PRECONDITIONS);
        HashMap<String, String> params = new HashMap<>();
        params.put("subsCode", subsCode);
        params.put("bizCode", bizCode);
        params.put("subBizCode", subBizCode);
        return communicator.get(params, type);
    }

    /**
     * 查询工单可执行的事件
     *
     * @param cxt
     * @param orderCode 订单项编码
     * @return
     * @throws Exception
     */
    public static List<Object> getOrderEvents(Context cxt, String orderCode) throws Exception {
        Type type = new TypeToken<List<Object>>() {
        }.getType();
        Communicator<List<Object>> communicator = new Communicator<>(cxt, String.format(ORDER_EVENTS, orderCode));
        return communicator.get(type);
    }

    /**
     * 获取签订合同url
     *
     * @param cxt
     * @param type      串码类型
     * @param expiresIn 串码有效期，单位为秒
     * @param subsCode  串码值(户号)
     * @return
     * @throws Exception
     */
    public static String getContractUrl(Context cxt, String type, int expiresIn, String subsCode) throws Exception {
//        RandomCode randomCode = RandomCodeRequest.getRandomCode(cxt, type, expiresIn, subsCode);
//        return String.format(CONTRACT_URL, randomCode.getCode());
        return "";
    }
}


